<?php

namespace ShowStop\TelevisionBundle\Controller;

use ShowStop\TelevisionBundle\Entity\Ratings;

use Symfony\Component\Security\Core\SecurityContext;

use Symfony\Component\Validator\Constraints\Date;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class DefaultController extends Controller
{
     
    public function seriesAction($id) {
    	$em = $this->getDoctrine()->getEntityManager();
    	
    	$query = $em->createQuery( 'SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.id = ' . $id );
    	
    	$series = $query->getResult();
    	
    	$user = $this->get('security.context')->getToken()->getUser();
    
    	if($user !== 'anon.')
    	{
	    	$values['hdnRating'] = 0;
	    	$values['hdnUserID'] = $this->get('security.context')->getToken()->getUser()->getId();
	    	$values['hdnSeriesID'] = $id;
	    	
	    	//create rating form
	    	$form = $this->createFormBuilder($values)
	    	->add('hdnRating', 'hidden')
	    	->add('hdnUserID', 'hidden')
	    	->add('hdnSeriesID', 'hidden')
	    	->getForm();
    	}    	
    	
    	$OverallRatingclass = "nostar";
    	$query = $em->createQuery('select avg(r.rating) from ShowStopTelevisionBundle:Ratings r where r.seriesid = '. $id);
    	$result = $query->getSingleScalarResult();
    	
    	if($result != null)
    	{
    		if($result > 0.5 and $result < 1.5)
    		{
    			$OverallRatingclass = "onestar";
    		}
    		else if ($result >= 1.5 and $result < 2.5 )
    		{
    			$OverallRatingclass = "twostar";
    		}
    		else if ($result >= 2.5 and $result < 3.5 )
    		{
    			$OverallRatingclass = "threestar";
    		}
    		else if ($result >= 3.5 and $result < 4.5 )
    		{
    			$OverallRatingclass = "fourstar";
    		}
    		else if ($result >= 4.5 )
    		{
    			$OverallRatingclass = "fivestar";
    		}  
    	}
    	
    	if($user !== 'anon.')
    	{
    		$UserRatingclass = "nostar";
    		$query = $em->createQuery('select r.rating from ShowStopTelevisionBundle:Ratings r where r.seriesid = '. $id .'and r.userid =' . $values['hdnUserID']);
    		$result = $query->getSingleScalarResult();
    		 
    		if($result != null)
    		{
    			if($result > 0.5 and $result < 1.5)
    			{
    				$UserRatingclass = "onestar";
    			}
    			else if ($result >= 1.5 and $result < 2.5 )
    			{
    				$UserRatingclass = "twostar";
    			}
    			else if ($result >= 2.5 and $result < 3.5 )
    			{
    				$UserRatingclass = "threestar";
    			}
    			else if ($result >= 3.5 and $result < 4.5 )
    			{
    				$UserRatingclass = "fourstar";
    			}
    			else if ($result >= 4.5 )
    			{
    				$UserRatingclass = "fivestar";
    			}
    		}    
    	
    		return $this->render('ShowStopTelevisionBundle:Default:series.html.twig', array('series' => $series, 'form' => $form->createView(), 'OverallRating' => $OverallRatingclass, 'UserRating' => $UserRatingclass ) );
    	}
    	else
    	{
    		return $this->render('ShowStopTelevisionBundle:Default:series.html.twig', array('series' => $series, 'OverallRating' => $OverallRatingclass ) );
    	}
    }
    
    public function add_ratingAction(Request $request)
    {
    	
    	$form = $this->createFormBuilder( array() )
    		->add('hdnRating', 'hidden')
    		->add('hdnUserID', 'hidden')
    		->add('hdnSeriesID', 'hidden')
    		->getForm();
    	
    	if ($request->getMethod() == 'POST') {
    	
    		$form->bindRequest($request);
    		$data = $form->getData();
    		 
	    	$em = $this->getDoctrine()->getEntityManager();
	    	 
	    	$query = $em->createQuery( 'SELECT r FROM ShowStopTelevisionBundle:Ratings r WHERE r.seriesid = ' .$data['hdnSeriesID'] .'and r.userid =' .$data['hdnUserID']);
	    	 
	    	$usrRating = $query->getResult();
	    	
	    	//add new rating
	    	if(empty($usrRating))
	    	{
	    		$rating = new Ratings();
	    		$rating->setSeriesId($data['hdnSeriesID']);
	    		$rating->setUserId($data['hdnUserID']);
	    		$rating->setRating($data['hdnRating']);
	    		$em->persist($rating);
	    		$em->flush();
	    		
	    	}
	    	else //update existing rating
	    	{
	    		$rating = $usrRating[0];
	    		$rating->setRating($data['hdnRating']);
	    		$em->persist($rating);
	    		$em->flush();
	    		    		
	    	}
	    	
    	}
    	return $this->forward('ShowStopTelevisionBundle:Default:series', array('id' => $data['hdnSeriesID'] ));
    }
    
    public function searchAction(Request $request) {
    	
    	$form = $this->createFormBuilder( array() )
    			->add('searchBox', 'text')
    			->getForm();
    	
    	$query = "No Search Query Entered";
    	
    	
    	
    	if( $request->getMethod() == 'POST' ) {
    		
    		$form->bindRequest($request);
    		$data = $form->getData();
    		$showName = $data["searchBox"];
    		
    		$em = $this->getDoctrine()->getEntityManager();
    		 
    		$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.name LIKE '%" . $showName ."%'");
    		 
    		$results = $query->getResult();

    	}
    	
    	return $this->render('ShowStopTelevisionBundle:Default:search.html.twig', array('name' => $showName, 'results' => $results ) );
    }
    
    public function searchAllAction( $index) {    	
    	
    	$query = "No Search Query Entered"; 	
    	
    	$em = $this->getDoctrine()->getEntityManager();

    	if($index == '0' ){
    		$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s ");
    		$results = $query->getResult();
    	}
    	else if ( $index == '#'){
    		
    		$conn = $this->get('database_connection');
    		$results = $conn->fetchAll( "SELECT * FROM series WHERE name REGEXP '^[?!@#$%^&*0-9]'");		
    		
    	}
    	else {
    		$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.name like '". $index ."%'");
    		$results = $query->getResult();
    	}
    	
    	$headerLinks = array("#","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    	
    	return $this->render('ShowStopTelevisionBundle:Default:searchAll.html.twig', array( 'headerLinks'=>$headerLinks, 'results' => $results, 'index' => $index ) );
    }
    
    public function upcomingAction() {
    	$em = $this->getDoctrine()->getEntityManager();
    	
    	$query = $em->createQuery( 'SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.return_date >= \'' . date("Y-m-d") . '\' ORDER BY s.return_date' );
    	
    	$series = $query->getResult();
    	
    	/*$DatedSeries = array();
    	$CurrentDate = $series[0]->getReturnDate();
    	$ArrayIndex = 0;
    	$DateIndex = 0;
    	
    	for( $i = 0; $i < count($series); $i++ )
    	{
    		if( $CurrentDate == $series[$i]->getReturnDate() )
    		{
    			$DatedSeries[$DateIndex][$ArrayIndex] = $series[$i];
    			++$ArrayIndex;
    		}
    		++$DateIndex;
    	}*/
    	
    	return $this->render('ShowStopTelevisionBundle:Default:upcoming.html.twig', array('series' => $series, 'oldDate' => '' ) );
    }
    
	
}
